package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.aspectj.bridge.Message;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: @Weiney
 * @ClassName: SetmealServiceImpl
 * @Description:
 * @Create: 2025-01-22
 **/
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐和套餐菜品关系
     * @param setmealDTO
     */
    @Transactional // 多表操作
    public void saveWithSetmealDishes(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 套餐表添加套餐记录
        setmealMapper.insert(setmeal);
        // 获取insert语句生成的主键值
        Long setmealId = setmeal.getId();
        // 获取SetmealDTO中套餐包含的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0) {

            setmealDishes.forEach(setmealDish -> {
                // 给套餐中的每个菜品赋值套餐id
                setmealDish.setSetmealId(setmealId);
            });

            // 套餐-菜品关系表添加菜品关系记录
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 套餐启售/禁售
     * @param status
     * @param id
     */
    public void changeStatus(Integer status, Long id) {
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        // 套餐中如果有停售菜品，是无法启用的
        // 获得套餐中菜品列表
        List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealDishesBySetmealId(id);
        setmealDishes.forEach(setmealDish -> {
            // 根据套餐菜品的dish_id获得菜品
            Dish dish = dishMapper.getById(setmealDish.getDishId());
            if(dish.getStatus() == StatusConstant.DISABLE){ // 该菜品停售
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        });

        setmealMapper.update(setmeal);
    }

    /**
     * 批量删除套餐
     * @param ids：套餐id列表
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        /*
         套餐表与套餐中包含的菜品表关联，删除套餐应保证：
            - 该套餐已停售
            - 删除套餐中包含的菜品记录 setmeal.id -> setmeal_dish.setmeal_id
            - 删除套餐表记录
        */
        for(Long id : ids) {
            // 根据id查询套餐
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus() == StatusConstant.ENABLE){
                // 在售套餐不能删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        setmealDishMapper.deleteBySetmealIds(ids);

        setmealMapper.deleteByIds(ids);
    }

    /**
     * 更新套餐信息和套餐内菜品信息
     * @param setmealDTO
     */
    @Transactional
    public void updateSetmealWithSetmealDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal); // 更新套餐信息

        // 根据套餐id删除套餐内菜品，然后添加套餐内菜品
        setmealDishMapper.deleteBySetmealId(setmeal.getId());

        // 批量新增套餐内菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId()); // 套餐内菜品赋值套餐id
            });
            // 批量新增
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    public SetmealVO getByIdWithSetmealDish(Long id) {
        // 查询套餐数据
        Setmeal setmeal = setmealMapper.getById(id);
        // 根据套餐id查询套餐内菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealDishesBySetmealId(setmeal.getId());
        // 封装VO
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}